package com.hanter.android.saf;

import android.Manifest;
import android.net.Uri;

import androidx.activity.ComponentActivity;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresPermission;
import androidx.fragment.app.Fragment;

import java.io.File;
import java.io.IOException;

public interface SafManager {

    /**
     * ${@link android.os.Environment#DIRECTORY_AUDIOBOOKS}
     */
    String DIRECTORY_AUDIOBOOKS = "Audiobooks";

    /**
     * 放置截图的标准目录，通常放在 ${@link android.os.Environment#DIRECTORY_PICTURES}
     */
    String DIRECTORY_SCREENSHOTS = "Screenshots";

    /**
     * 注册 授权 Uri 修改的 StartIntentSenderForResult
     */
    void register(Fragment fragment);

    /**
     * 注册 授权 Uri 修改的 StartIntentSenderForResult
     */
    void register(ComponentActivity activity);

    /**
     * 获取对应类型的路径Uri，在MediaStore Api中使用
     */
    Uri getMediaContentUri(@MediaType String type);

    Uri getMediaContentUri(@MediaType String type, boolean external);

    @RequiresPermission(value = Manifest.permission.READ_EXTERNAL_STORAGE)
    @Nullable
    Uri getMediaUri(@MediaType String type, String fileName);

    @RequiresPermission(value = Manifest.permission.READ_EXTERNAL_STORAGE)
    boolean exists(@Nullable Uri uri);

    @RequiresPermission(value = Manifest.permission.READ_EXTERNAL_STORAGE)
    boolean exists(@MediaType String type, String fileName);

    /**
     * 创建指定类型目录下的文件
     * @param type Environment.DIRECTORY_
     * @param fileName 文件名，可以包含路径
     */
    @RequiresPermission(value = Manifest.permission.WRITE_EXTERNAL_STORAGE, conditional = true)
    @Nullable
    Uri createMediaUri(@MediaType String type, String fileName) throws IOException;

    /**
     * 创建指定类型目录下的文件
     * @param type Environment.DIRECTORY_
     * @param fileName 文件名，可以包含路径
     * @param mimeType 文件的mimeType，可以为null，会根据type生成默认类型
     */
    @RequiresPermission(value = Manifest.permission.WRITE_EXTERNAL_STORAGE, conditional = true)
    @Nullable
    Uri createMediaUri(@MediaType String type, String fileName, String mimeType) throws IOException;

    /**
     * 创建图片Uri
     * @param fileName 图片文件名
     */
    @RequiresPermission(value = Manifest.permission.WRITE_EXTERNAL_STORAGE, conditional = true)
    @Nullable
    Uri createImageUri(String fileName) throws IOException;

    /**
     * 创建图片Uri
     * @param fileName 图片文件名
     * @param mimeType 图片的mimeType，传空默认 image/jpeg
     * @throws IOException 当没有外部存储时，IO 异常
     */
    Uri createImageUri(String fileName, String mimeType) throws IOException;

    @RequiresPermission(value = Manifest.permission.WRITE_EXTERNAL_STORAGE, conditional = true)
    boolean delete(@NonNull Uri uri);

    @RequiresPermission(value = Manifest.permission.WRITE_EXTERNAL_STORAGE, conditional = true)
    void delete(@NonNull Uri uri, @NonNull SafManagerImpl.OnDeleteCallback callback);

    @RequiresPermission(value = Manifest.permission.WRITE_EXTERNAL_STORAGE, conditional = true)
    boolean delete(@NonNull Uri contentUri, long rowId);

    /**
     * 获取App私有的媒体目录
     * @param type {@link android.os.Environment} DIRECTORY_ 开头的目录
     * @param external 是否外部存储
     * @return 媒体目录
     */
    File getAppMediaDirectory(@MediaType String type, boolean external);

    /**
     * 获取App的Files目录，使用内部还是外部
     * @param external 内部存储
     */
    File getAppFilesDirectory(boolean external);

}
